

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/boshi.jpeg">
  <link rel="icon" type="image/png" href="/img/boshi.jpeg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="wangpf">
  <meta name="keywords" content="">
  <title>TypeScript 学习总结之 泛型 - wpf的博客</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/foundation.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_6peoq002giu.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.1.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Wangpf's blog</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/article.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-12-30 18:22" pubdate>
      2020年12月30日 晚上
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.5k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      21
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">TypeScript 学习总结之 泛型</h1>
            
            <div class="markdown-body" id="post-body">
              <p>​<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/408cfbc308734bd8bcbb8a36c40135d0~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="TypeScript"></p>
<h2 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h2><p>在像C#和Java这样的语言中，可以使用泛型来创建可重用的组件，一个组件可以支持多种类型的数据。 这样用户就可以以自己的数据类型来使用组件。</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-comment">// T : 泛型变量 | 类型变量</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">identity</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">arg: T</span>): <span class="hljs-title">T</span> </span>&#123;
  <span class="hljs-keyword">return</span> arg;
&#125;
identity(<span class="hljs-number">123</span>)
identity(<span class="hljs-string">&#x27;123&#x27;</span>)</code></pre></div>

<p>代码中 <strong>T 代表 「Type」</strong>，在定义泛型时通常用作第一个类型变量名称。但实际上 T 可以用任何有效名称代替。</p>
<p>由于我们操作的是数组，所以.length属性是应该存在的。 我们可以像创建其它数组一样创建这个数组：</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">loggingIdentity</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">arg: T[]</span>): <span class="hljs-title">T</span>[] </span>&#123;
  <span class="hljs-built_in">console</span>.log(arg.length);  <span class="hljs-comment">// Array has a .length, so no more error</span>
  <span class="hljs-keyword">return</span> arg;
&#125;</code></pre></div>



<p>其实并不是只能定义一个类型变量，我们可以引入希望定义的任何数量的类型变量。比如我们引入一个新的类型变量 <code>U</code>，用于扩展我们定义的 <code>identity</code> 函数：</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">identity</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">U</span>&gt;(<span class="hljs-params">value: T, message: U</span>): <span class="hljs-title">T</span> </span>&#123;
  <span class="hljs-built_in">console</span>.log(message);
  <span class="hljs-keyword">return</span> value;
&#125;

<span class="hljs-built_in">console</span>.log(identity(<span class="hljs-number">666</span>, <span class="hljs-string">&quot;jack is cool!&quot;</span>));</code></pre></div>


<h2 id="泛型类型-amp-amp-泛型接口"><a href="#泛型类型-amp-amp-泛型接口" class="headerlink" title="泛型类型 &amp;&amp; 泛型接口"></a>泛型类型 &amp;&amp; 泛型接口</h2><p>泛型类型不同的注解方式:</p>
<ol>
<li>函数泛型的注解方式</li>
<li>对象字面量的方式来定义泛型类型</li>
<li>泛型接口的定义方式<div class="hljs"><pre><code class="hljs ts"><span class="hljs-comment">//  泛型类型的不同方式</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">identity</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">arg: T</span>): <span class="hljs-title">T</span> </span>&#123;
  <span class="hljs-keyword">return</span> arg;
&#125;

<span class="hljs-comment">// 1. 函数泛型的注解方式：</span>
<span class="hljs-keyword">let</span> a: &lt;T&gt;<span class="hljs-function">(<span class="hljs-params">arg: T</span>) =&gt;</span> T = identity

<span class="hljs-comment">// 2. 对象字面量的方式来定义泛型类型</span>
<span class="hljs-keyword">let</span> b: &#123; &lt;T&gt;(arg: T): T &#125; = identity

<span class="hljs-comment">// 3. 泛型接口的定义方式</span>
<span class="hljs-keyword">interface</span> IdentityInterface &#123;
  &lt;T&gt;(arg: T): T
&#125;
<span class="hljs-keyword">let</span> c: IdentityInterface = identity</code></pre></div>

</li>
</ol>
<h2 id="泛型类-amp-泛型约束"><a href="#泛型类-amp-泛型约束" class="headerlink" title="泛型类 &amp; 泛型约束"></a>泛型类 &amp; 泛型约束</h2><h3 id="泛型类"><a href="#泛型类" class="headerlink" title="泛型类"></a>泛型类</h3><p>泛型类看上去与泛型接口差不多，我们只需要在类名后面，使用 &lt;T, …&gt; 的语法定义任意多个类型变量，具体示例如下：</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-comment">// 泛型类</span>
<span class="hljs-keyword">class</span> MinClass&lt;T&gt;&#123;
  <span class="hljs-keyword">public</span> list: T[] = []
  add(num: T) &#123;
    <span class="hljs-built_in">this</span>.list.push(num)
  &#125;
  min(): T &#123;
    <span class="hljs-keyword">let</span> minNum = <span class="hljs-built_in">this</span>.list[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.list.length; i++) &#123;
      <span class="hljs-keyword">if</span> (minNum &gt; <span class="hljs-built_in">this</span>.list[i]) &#123;
        minNum = <span class="hljs-built_in">this</span>.list[i]
      &#125;
    &#125;
    <span class="hljs-keyword">return</span> minNum
  &#125;
&#125;</code></pre></div>

<p>我们在什么时候需要使用泛型呢？通常在决定是否使用泛型时，我们有以下两个参考标准：</p>
<ul>
<li>当你的函数、接口或类将处理多种数据类型时；</li>
<li>当函数、接口或类在多个地方使用该数据类型时。</li>
</ul>
<h3 id="泛型约束"><a href="#泛型约束" class="headerlink" title="泛型约束"></a>泛型约束</h3><p><strong>有时我们可能希望限制每个类型变量接受的类型数量，这就是泛型约束的作用。</strong></p>
<p>以官方文档例子为例：</p>
<p>我们需要去定义一个接口来描述约束条件。</p>
<p>创建一个包含 .length属性的接口，使用这个接口和extends关键字来实现约束：</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">interface</span> LengthInterface &#123;
  length: <span class="hljs-built_in">number</span>
&#125;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">loggingIdentity</span>&lt;<span class="hljs-title">T</span> <span class="hljs-title">extends</span> <span class="hljs-title">LengthInterface</span>&gt;(<span class="hljs-params">arg: T</span>): <span class="hljs-title">T</span> </span>&#123;
  <span class="hljs-built_in">console</span>.log(arg.length);
  <span class="hljs-keyword">return</span> arg;
&#125;</code></pre></div>

<p>其中,<strong>泛型约束用的是： extends 继承接口的方式（不一定非要是接口）</strong> 、<code>T extends LengthInterface </code>用于告诉编译器，我们支持已经实现 Length 接口的任何类型</p>
<p>另外， 泛型约束并不一定用接口方式， 比如 我们可以把以上代码 接口 换成 类型别名 ,<br>如下例子：<br>// 以类型别名的方式依然可以</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">type</span> LengthType = <span class="hljs-built_in">string</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">loggingIdentity</span>&lt;<span class="hljs-title">T</span> <span class="hljs-title">extends</span> <span class="hljs-title">LengthType</span>&gt;(<span class="hljs-params">arg: T</span>): <span class="hljs-title">T</span> </span>&#123;
  <span class="hljs-built_in">console</span>.log(arg.length);
  <span class="hljs-keyword">return</span> arg;
&#125;</code></pre></div>

<h4 id="keyof-操作符"><a href="#keyof-操作符" class="headerlink" title="keyof 操作符"></a>keyof 操作符</h4><p><strong>keyof操作符可以用于获取某种类型的所有键，其返回类型是联合类型。</strong></p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-comment">// keyof 操作符</span>

<span class="hljs-keyword">interface</span> Person &#123;
  name: <span class="hljs-built_in">string</span>;
  age: <span class="hljs-built_in">number</span>;
  location: <span class="hljs-built_in">string</span>;
&#125;

<span class="hljs-keyword">type</span> K1 = keyof Person; <span class="hljs-comment">// &quot;name&quot; | &quot;age&quot; | &quot;location&quot;</span>
<span class="hljs-keyword">type</span> K2 = keyof Person[];  <span class="hljs-comment">// number | &quot;length&quot; | &quot;push&quot; | &quot;concat&quot; | ...</span>
<span class="hljs-keyword">type</span> K3 = keyof &#123; [x: <span class="hljs-built_in">string</span>]: Person &#125;;  <span class="hljs-comment">// string | number</span></code></pre></div>
<p>可以看出， k1,k2,k3其实是键名</p>
<h4 id="在泛型约束中使用类型参数"><a href="#在泛型约束中使用类型参数" class="headerlink" title="在泛型约束中使用类型参数"></a>在泛型约束中使用类型参数</h4><p>当我们理解了 keyof 操作符时， 看下面代码就很好理解了</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getProperty</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">K</span> <span class="hljs-title">extends</span> <span class="hljs-title">keyof</span> <span class="hljs-title">T</span>&gt;(<span class="hljs-params">obj: T, key: K</span>) </span>&#123;
  <span class="hljs-keyword">return</span> obj[key];
&#125;

<span class="hljs-keyword">let</span> x = &#123; a: <span class="hljs-number">1</span>, b: <span class="hljs-number">2</span>, c: <span class="hljs-number">3</span>, d: <span class="hljs-number">4</span> &#125;;

getProperty(x, <span class="hljs-string">&quot;a&quot;</span>); <span class="hljs-comment">// okay</span>
getProperty(x, <span class="hljs-string">&quot;m&quot;</span>); <span class="hljs-comment">// error: Argument of type &#x27;m&#x27; isn&#x27;t assignable to &#x27;a&#x27; | &#x27;b&#x27; | &#x27;c&#x27; | &#x27;d&#x27;.</span></code></pre></div>
<p>**而 K 就是指  ‘a’ , ‘b’ , ‘c’ , ‘d’ **</p>
<p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6c050952b0be44d5bd16b959967b04ac~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="K"></p>
<p>很明显通过使用泛型约束，在编译阶段我们就可以提前发现错误，大大提高了程序的健壮性和稳定性。</p>
<h3 id="多重泛型约束-amp-交叉类型"><a href="#多重泛型约束-amp-交叉类型" class="headerlink" title="多重泛型约束 &amp;  交叉类型"></a>多重泛型约束 &amp;  交叉类型</h3><p>交叉类型是将多个类型合并为一个类型。 这让我们可以把现有的多种类型叠加到一起成为一种类型，它包含了所需的所有类型的特性。</p>
<div class="hljs"><pre><code class="hljs typescript">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">extend</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">U</span>&gt;(<span class="hljs-params">first: T, second: U</span>): <span class="hljs-title">T</span> &amp; <span class="hljs-title">U</span> </span>&#123;
  <span class="hljs-keyword">let</span> result = &lt;T &amp; U&gt;&#123;&#125;;  <span class="hljs-comment">// 断言， 表示 result 包含 T  U 俩种类型</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> id <span class="hljs-keyword">in</span> first) &#123;  
    (&lt;<span class="hljs-built_in">any</span>&gt;result)[id] = (&lt;<span class="hljs-built_in">any</span>&gt;first)[id];   <span class="hljs-comment">// 将 first 中所有属性 给 result</span>
  &#125;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> id <span class="hljs-keyword">in</span> second) &#123;
    <span class="hljs-keyword">if</span> (!result.hasOwnProperty(id)) &#123;
      (&lt;<span class="hljs-built_in">any</span>&gt;result)[id] = (&lt;<span class="hljs-built_in">any</span>&gt;second)[id];  
      <span class="hljs-comment">// 将 second 中所有属性 给 result, 前提是result 没有该属性时</span>
    &#125;
  &#125;
  <span class="hljs-keyword">return</span> result;   <span class="hljs-comment">// 返回的 result 就有  first 和 second 的所有属性</span>
&#125;


<span class="hljs-keyword">class</span> Person &#123;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">public</span> name: <span class="hljs-built_in">string</span></span>) &#123; &#125;
&#125;
<span class="hljs-keyword">interface</span> Loggable &#123;
  log(): <span class="hljs-built_in">void</span>;
&#125;
<span class="hljs-keyword">class</span> ConsoleLogger <span class="hljs-keyword">implements</span> Loggable &#123;
  log() &#123;
    <span class="hljs-comment">// ...</span>
  &#125;
&#125;
<span class="hljs-keyword">var</span> jim = extend(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">&quot;Jim&quot;</span>), <span class="hljs-keyword">new</span> ConsoleLogger());
<span class="hljs-keyword">var</span> n = jim.name;
jim.log();
</code></pre></div>

<p>再举个简单例子：</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">interface</span> Sentence &#123;
  content: <span class="hljs-built_in">string</span>
  title: <span class="hljs-built_in">string</span>
&#125;

<span class="hljs-keyword">interface</span> Music &#123;
  url: <span class="hljs-built_in">string</span>
&#125;

<span class="hljs-keyword">class</span> Test&lt;T <span class="hljs-keyword">extends</span> Sentence &amp; Music&gt;&#123;
  props: T
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">public</span> arg: T</span>) &#123;
    <span class="hljs-built_in">this</span>.props = arg
  &#125;

  info() &#123;
    <span class="hljs-keyword">return</span> &#123;
    <span class="hljs-comment">// 这里可以 this.props.xxx 的原因是因为 arg 符合 T类型， 而T 又继承了上面俩个接口的属性。</span>
      content: <span class="hljs-built_in">this</span>.props.content,
      title: <span class="hljs-built_in">this</span>.props.title,
      url: <span class="hljs-built_in">this</span>.props.url
    &#125;
  &#125;
&#125;</code></pre></div>

<h3 id="泛型中的类类型"><a href="#泛型中的类类型" class="headerlink" title="泛型中的类类型"></a>泛型中的类类型</h3><p><strong>泛型中的类类型 目的是 约束或者更好的推论</strong></p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> BeeKeeper &#123;
  hasMask: <span class="hljs-built_in">boolean</span>;
&#125;

<span class="hljs-keyword">class</span> ZooKeeper &#123;
  nametag: <span class="hljs-built_in">string</span>;
&#125;

<span class="hljs-keyword">class</span> Animal &#123;
  numLegs: <span class="hljs-built_in">number</span>;
&#125;

<span class="hljs-keyword">class</span> Bee <span class="hljs-keyword">extends</span> Animal &#123;
  keeper: BeeKeeper;
&#125;

<span class="hljs-keyword">class</span> Lion <span class="hljs-keyword">extends</span> Animal &#123;
  keeper: ZooKeeper;
&#125;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createInstance</span>&lt;<span class="hljs-title">A</span> <span class="hljs-title">extends</span> <span class="hljs-title">Animal</span>&gt;(<span class="hljs-params">c: <span class="hljs-keyword">new</span> () =&gt; A</span>): <span class="hljs-title">A</span> </span>&#123;
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> c();
&#125;

createInstance(Lion).keeper.nametag;  <span class="hljs-comment">// typechecks!</span>
createInstance(Bee).keeper.hasMask;   <span class="hljs-comment">// typechecks!</span></code></pre></div>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                <div class="post-meta mr-3">
                  <i class="iconfont icon-category"></i>
                  
                  <a class="hover-with-bg" href="/categories/typescript/">typescript</a>
                  
                  <a class="hover-with-bg" href="/categories/typescript/JavaScript/">JavaScript</a>
                  
                </div>
                
                
                <div class="post-meta">
                  <i class="iconfont icon-tags"></i>
                  
                  <a class="hover-with-bg" href="/tags/typescript/">typescript</a>
                  
                </div>
                
              </div>
              
              
              <div class="post-prevnext row">
                <article class="post-prev col-6">
                  
                  
                  <a href="/2021/01/05/44-%E5%9C%A8Node.js%E4%B8%8A%E5%81%9A%E4%B8%80%E4%B8%AA%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%93%8D%E4%BD%9Ctodo/">
                    <i class="iconfont icon-arrowleft"></i>
                    <span class="hidden-mobile">在Node.js上做一个使用命令行操作todo</span>
                    <span class="visible-mobile">上一篇</span>
                  </a>
                  
                </article>
                <article class="post-next col-6">
                  
                  
                  <a href="/2020/12/29/42.TypeScript%E7%B3%BB%E5%88%974/">
                    <span class="hidden-mobile">TypeScript 学习总结之 接口</span>
                    <span class="visible-mobile">下一篇</span>
                    <i class="iconfont icon-arrowright"></i>
                  </a>
                  
                </article>
              </div>
              
            </div>

            
            <!-- Comments -->
            <article class="comments" id="comments">
              
              
  <div id="vcomments"></div>
  <script type="text/javascript">
    function loadValine() {
      addScript('https://cdn.staticfile.org/valine/1.4.14/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "hjxsDRXYScicliqD8I8JqoTu-gzGzoHsz",
          app_key: "qUcAEReEqeEVp5wwXJ41uCCp",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    }
    waitElementVisible('vcomments', loadValine);
  </script>
  <noscript>Please enable JavaScript to view the <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments
      powered by Valine.</a></noscript>


            </article>
            
          </article>
        </div>
      </div>
    </div>
    
    <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
      <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

    </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "TypeScript 学习总结之 泛型&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "always",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  











  

  

  

  

  

  





</body>
</html>
